/******************************************************************************
 *      Mestrado em Ciência da Computação - UFMS
 *		Redes de Computadores - Prof. Dr. Ronaldo Alves Ferreira
 *		Alunos: Geraldo Landre, Marcel Tolentino, Vinícius Bueno
 *****************************************************************************/
/* tcpout.c - tcpout */

#include "tcpfms.h"
#include "tcb.h"
#include "tcp.h"
#include "port.h"
#include "my_socket.h"
#include "tcbtab.h"

/*------------------------------------------------------------------------
 *  tcpout - handle events affecting TCP output processing
 *------------------------------------------------------------------------
 */

// verificar
void tcpout()
{
    struct	tcb	*ptcb;
    int i;
    for(;;)
    {
        i = port_recvmsg(thread_getport(output_thread));
        ptcb = (struct tcb*)&tcbtab[TCB(i)]; // ???
        if (ptcb->tcb_state <= TCPS_CLOSED)
            continue;		// a rogue; ignore it
        sem_wait(&(ptcb->tcb_mutex));
        if (ptcb->tcb_state <= TCPS_CLOSED)
            continue;		// TCB deallocated
        if (EVENT(i) == DEL)		// same for all states
            tcbdealloc(ptcb);
        else
            tcposwitch[ptcb->tcb_ostate](TCB(i), EVENT(i));
        if (ptcb->tcb_state != TCPS_FREE)
            sem_post(&(ptcb->tcb_mutex));
    }
}
